home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / LOOKUP.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  57KB  |  1,724 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. #include "jwp.h"
  4. #ifdef CTL3D
  5.     #include <ctl3d.h>
  6. #endif CTL3D
  7.  
  8.  
  9. #define KANJIBOXBORDER    AVGWIDTH
  10. #define KANJILISTBORDER   BORDERSPACE
  11. #define SCROLLBLOCKS      5
  12. #define OKURIGANA         0x20
  13.  
  14. #define NRSORT  2
  15. #define MAXLIST 50
  16. #define STROKES 0
  17. #define BUSHU   1
  18.  
  19. typedef struct {
  20.     unsigned char strokes;
  21.     unsigned char bushu;
  22.     unsigned short int nelson;
  23.     unsigned char grade;
  24.     unsigned char on:4;
  25.     unsigned char kun:4;
  26.     unsigned char unknown:4;
  27.     unsigned char meanings:4;
  28.     unsigned char padding;              // To pad it to 32 bits
  29.     unsigned long int offset;
  30. } INFORECORD;
  31.  
  32. typedef struct {
  33.     unsigned char sortkeys[NRSORT];
  34.     KANJI jis;
  35. } IDXRECORD;
  36.  
  37. static OFSTRUCT kinfoidxof;
  38. static BOOL KinfoIdx = FALSE;
  39. static int KinfoMax = -1;
  40. static int SortKey;
  41. static int IdxBase, NrIndex;
  42. static int CurrentKeys[NRSORT];
  43. static int MaxKeys[NRSORT];
  44. static int fd;
  45.  
  46.  
  47. static IDXRECORD KanjiList[MAXLIST];
  48. static int start, stop;
  49. static int NrInList, NrSelected;
  50. static int EdgeGap;
  51.  
  52. static KANJI InfoChar;
  53.  
  54.  
  55. #define NRJISROWS  6
  56.  
  57. static int PageNum;
  58. static int SelRow, SelCol;
  59. static int RowDiv, ColDiv;
  60. static int RowPos, ColPos;
  61.  
  62. BOOL FAR PASCAL KanjiInfoProc (HWND, WORD, WORD, LONG);
  63.  
  64. static KANJI BushuSymbols[] = {
  65.     0x306c, 0x2143, 0x5026, 0x254e, 0x3235,     /* 01 - 05 */
  66.     0x502d, 0x4673, 0x5035, 0x3f4d, 0x5139,     /* 06 - 10 */
  67.     0x467e, 0x482c, 0x5144, 0x514c, 0x5152,     /* 11 - 15 */
  68.     0x515c, 0x5161, 0x4561, 0x4e4f, 0x5231,     /* 16 - 20 */
  69.     0x5238, 0x5239, 0x523e, 0x3d3d, 0x4b4e,     /* 21 - 25 */
  70.     0x5247, 0x524c, 0x5253, 0x4b74, 0x387d,     /* 26 - 30 */
  71.     0x5378, 0x455a, 0x3b4e, 0x5469, 0x546a,     /* 31 - 35 */
  72.     0x4d3c, 0x4267, 0x3d77, 0x3b52, 0x555f,     /* 36 - 40 */
  73.     0x4023, 0x3e2e, 0x5577, 0x5579, 0x4366,     /* 41 - 45 [ 45 qustionable ] */
  74.     0x3b33, 0x406e, 0x3929, 0x384a, 0x3652,     /* 46 - 50 */
  75.     0x3433, 0x5676, 0x5678, 0x572e, 0x5730,     /* 51 - 55 */
  76.     0x5735, 0x355d, 0x4730, 0x5744, 0x5746,     /* 56 - 60 [ 58 questionable ] */
  77.     0x3f34, 0x5879, 0x384d, 0x3c6a, 0x3b59,     /* 61 - 65 */
  78.     0x5a3d, 0x4a38, 0x454d, 0x3654, 0x4a7d,     /* 66 - 70 */
  79.     0x5a5b, 0x467c, 0x5b29, 0x376e, 0x4c5a,     /* 71 - 75 */
  80.     0x3767, 0x3b5f, 0x5d46, 0x5d55, 0x5d59,     /* 76 - 80 */
  81.     0x4866, 0x4c53, 0x3b61, 0x5d63, 0x3f65,     /* 81 - 85 */
  82.     0x3250, 0x445e, 0x4963, 0x602b, 0x602d,     /* 86 - 90 */
  83.     0x4a52, 0x3267, 0x356d, 0x3824, 0x383c,     /* 91 - 95 */
  84.     0x364c, 0x313b, 0x3424, 0x3445, 0x4038,     /* 96 - 100 */
  85.     0x4d51, 0x4544, 0x4925, 0x614b, 0x6222,     /* 101 - 105 [ 104 questionable ] */
  86.     0x4772, 0x4869, 0x3b2e, 0x4c5c, 0x4c37,     /* 106 - 110 */
  87.     0x4c70, 0x4050, 0x3c28, 0x633b, 0x3253,     /* 111 - 115 [ 114 questionable ] */
  88.     0x376a, 0x4e29, 0x435d, 0x4a46, 0x3b65,     /* 116 - 120 */
  89.     0x344c, 0x6626, 0x4d53, 0x3129, 0x4f37,     /* 121 - 125 */
  90.     0x3c29, 0x6650, 0x3c2a, 0x6666, 0x4679,     /* 126 - 130 */
  91.     0x3f43, 0x3c2b, 0x3b6a, 0x3131, 0x4065,     /* 131 - 135 */
  92.     0x4124, 0x3d2e, 0x3a31, 0x3f27, 0x6767,     /* 136 - 140 */
  93.     0x6948, 0x436e, 0x376c, 0x3954, 0x3061,     /* 141 - 145 */
  94.     0x403e, 0x382b, 0x3351, 0x3840, 0x432b,     /* 146 - 150 */
  95.     0x4626, 0x6c35, 0x6c38, 0x332d, 0x4056,     /* 151 - 155 */
  96.     0x4176, 0x422d, 0x3f48, 0x3c56, 0x3f49,     /* 156 - 160 */
  97.     0x4324, 0x6d68, 0x4d38, 0x4653, 0x4850,     /* 161 - 165 [ 162 questionable ] */
  98.     0x4e24, 0x3662, 0x4439, 0x4c67, 0x496c,     /* 166 - 170 */
  99.     0x7030, 0x7032, 0x312b, 0x4044, 0x4873,     /* 171 - 175 */
  100.     0x4c4c, 0x3357, 0x706a, 0x706c, 0x323b,     /* 176 - 180 */
  101.     0x4a47, 0x4977, 0x4874, 0x3f29, 0x3c73,     /* 181 - 185 */
  102.     0x3961, 0x474f, 0x397c, 0x3962, 0x7175,     /* 186 - 190 */
  103.     0x7228, 0x722e, 0x722f, 0x3534, 0x357b,     /* 191 - 195 */
  104.     0x443b, 0x7343, 0x3c2f, 0x734e, 0x4b63,     /* 196 - 200 */
  105.     0x322b, 0x3550, 0x3975, 0x7363, 0x7366,     /* 201 - 205 */
  106.     0x4524, 0x385d, 0x414d, 0x4921, 0x736e,     /* 206 - 210 */
  107.     0x736f, 0x4e36, 0x737d, 0x737e, 0x2121,     /* 211 - 215 */
  108.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 216 - 220 */
  109.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 221 - 225 */
  110.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 226 - 230 */
  111.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 231 - 235 */
  112.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 236 - 240 */
  113.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 241 - 245 */
  114.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121,     /* 246 - 250 */
  115.     0x2121, 0x2121, 0x2121, 0x2121, 0x2121      /* 251 - 255 */
  116. };
  117.  
  118.  
  119.  
  120.  
  121. void SetKanjiInfoChar (KANJI ch)
  122. {
  123.     InfoChar = ch;
  124. }
  125.  
  126.  
  127.  
  128. static int KeysComp (int keys[], IDXRECORD record, int SortKey)
  129. {
  130.     int i;
  131.  
  132.     if (keys[SortKey] != record.sortkeys[SortKey])
  133.         return (keys[SortKey] - record.sortkeys[SortKey]);
  134.  
  135.     for (i = 0; i < NRSORT; i++) {
  136.         if (i == SortKey) continue;
  137.         if (keys[i] != record.sortkeys[i])
  138.             return (keys[i] - record.sortkeys[i]);
  139.     }
  140.  
  141.     return (0);
  142. }
  143.  
  144.  
  145.  
  146. static int BinarySearchIndex (int fd, int which, BOOL lower)
  147. {
  148.     int top, bottom, middle;
  149.     int diff;
  150.     IDXRECORD record;
  151.  
  152.     /* Now binary search */
  153.  
  154.     top = 0;
  155.     bottom = NrIndex - 1;
  156.  
  157.     for (;;) {
  158.         middle = (top + bottom) / 2;
  159.  
  160.         lseek(fd, IdxBase * which + middle * sizeof(IDXRECORD), 0L);
  161.         read(fd, &record, sizeof(IDXRECORD));
  162.  
  163.         diff = KeysComp(CurrentKeys, record, which);
  164.  
  165.         if (diff == 0) {
  166.             /* Move to the beginning */
  167.             while (middle >= 0) {
  168.                 lseek(fd, IdxBase * which + middle * sizeof(IDXRECORD), 0);
  169.                 read(fd, &record, sizeof(IDXRECORD));
  170.                 if (KeysComp(CurrentKeys, record, which) > 0) break;
  171.                 middle--;
  172.             }
  173.  
  174.             return (middle + 1);
  175.         }
  176.  
  177.         if (top >= bottom - 1) return (lower ? bottom : top);
  178.  
  179.         if (diff > 0) top = middle;
  180.         else bottom = middle;
  181.     }
  182. }
  183.  
  184.  
  185.  
  186. static void PerformSearch (HWND hwnd, int fd, BOOL direction, BOOL MainKey)
  187. {
  188.     int i, j;
  189.     IDXRECORD record;
  190.     HCURSOR hCursor;
  191.  
  192.  
  193.     hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  194.     ShowCursor(TRUE);
  195.  
  196.     /* Now binary search the list */
  197.  
  198.     j = BinarySearchIndex(fd, SortKey, TRUE);
  199.     lseek(fd, IdxBase * SortKey + j * sizeof(IDXRECORD), 0);
  200.     read(fd, &record, sizeof(IDXRECORD));
  201.  
  202.     if (MainKey) {
  203.         if (!direction) {
  204.             if (CurrentKeys[SortKey] != record.sortkeys[SortKey]) j--;
  205.         }
  206.     } else {
  207.         if (direction) {
  208.             if (CurrentKeys[SortKey] != record.sortkeys[SortKey]) j--;
  209.         } else {
  210.             if (KeysComp(CurrentKeys, record, SortKey) < 0) {
  211.                 lseek(fd, IdxBase * SortKey + (j-1) * sizeof(IDXRECORD), 0);
  212.                 read(fd, &record, sizeof(IDXRECORD));
  213.                 if (CurrentKeys[SortKey] == record.sortkeys[SortKey]) j--;
  214.             }
  215.         }
  216.     }
  217.  
  218.  
  219.     start = j - NrSelected;
  220.     stop = start + NrInList - 1;
  221.  
  222.     lseek(fd, SortKey * IdxBase, 0);
  223.     if (start > 0) lseek(fd, start * sizeof(IDXRECORD), 1);
  224.  
  225.     for (j = 0; j < NrInList; j++) {
  226.         if (start + j < 0 || start + j >= NrIndex) {
  227.             KanjiList[j].jis = 0;
  228.         } else {
  229.             read(fd, &record, sizeof(IDXRECORD));
  230.             KanjiList[j] = record;
  231.         }
  232.  
  233.         if (j != NrSelected) continue;
  234.  
  235.         for (i = 0; i < NRSORT; i++) {
  236.             if (record.sortkeys[i] != CurrentKeys[i]) {
  237.                 CurrentKeys[i] = record.sortkeys[i];
  238.                 SetScrollPos(GetDlgItem(hwnd, 4221 + i), SB_CTL, CurrentKeys[i], TRUE);
  239.                 SetDlgItemInt(hwnd, 4231 + i, CurrentKeys[i], TRUE);
  240.             }
  241.         }
  242.     }
  243.  
  244.     InvalidateRect(GetDlgItem(hwnd, 4201), NULL, FALSE);
  245.     SendDlgItemMessage(hwnd, 4201, WM_USER, 0, 0L);
  246.  
  247.     ShowCursor(FALSE);
  248.     SetCursor(hCursor);
  249. }
  250.  
  251.  
  252.  
  253. BOOL FAR PASCAL LookupProc (HWND hwnd, WORD message, WORD wParam,